﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TranTekBE.Trac_Nghiem
{
    public partial class DanhMucCauHoi : PageRequireLogin
    {
        public Database database = new Database(TinyLib.GetConfigKey("ConnectionString2"));
        protected void Page_Load(object sender, EventArgs e)
        {
            LiteralSuccess.Text = "";
            LiteralError.Text = "";
            CheckerSelected();
            if (!IsPostBack)
            {
                LoadData();
            }

        }

        void LoadData()
        {
            var ds = database.ExecuteQuery("SELECT *  FROM  tbl_DailyCategory ");
            if (ds.Tables.Count > 0)
            {
                var dt = ds.Tables[0];
                if (dt.Rows.Count > 0)
                {
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
            }
            CheckerSelected();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                string sql = string.Format("INSERT INTO tbl_DailyCategory (Name,Description,IsActive) VALUES(N'{0}',N'{1}',1)", txtName.Text, txtDes.Text);
                database.ExecuteQuery(sql);
                LoadData();
                LiteralSuccess.Text = "Thêm thành công.";
                GridView1.SelectedIndex = -1;

            }
            catch(Exception ex)
            {
                LiteralError.Text ="Lỗi: "+ ex.Message;
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                HiddenField hf = (HiddenField)GridView1.Rows[GridView1.SelectedIndex].FindControl("CategoryId");
                string sql = string.Format("UPDATE tbl_DailyCategory SET Name=N'{0}',Description=N'{1}' WHERE CategoryId={2}", txtName.Text, txtDes.Text,hf.Value);
                database.ExecuteQuery(sql);
                LoadData();
                LiteralSuccess.Text = "Sửa thành công.";
            }
            catch (Exception ex)
            {
                LiteralError.Text = "Lỗi: " + ex.Message;
            }
        }

        protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            foreach (GridViewRow item in GridView1.Rows)
            {
                item.CssClass = "";
            }
            GridView1.Rows[e.NewSelectedIndex].CssClass = "selected";
            CheckerSelected();

            HiddenField hf = (HiddenField)GridView1.Rows[e.NewSelectedIndex].FindControl("CategoryId");
           // HiddenField hf = GridView1.SelectedRow.FindControl("CategoryId") as HiddenField;
            string sql = "SELECT * FROM tbl_DailyCategory WHERE CategoryId="+hf.Value;
            var ds = database.ExecuteQuery(sql);
            if (ds.Tables.Count > 0)
            {
                var dt = ds.Tables[0];
                if (dt.Rows.Count > 0)
                {
                    txtName.Text = dt.Rows[0]["Name"].ToString();
                    txtDes.Text = dt.Rows[0]["Description"].ToString();
                }
            }
        }
        void CheckerSelected()
        {
            if (GridView1.SelectedIndex < 0)
            {
                btnUpdate.Enabled = false;
            }
            else {
                btnUpdate.Enabled = true;
            }
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                HiddenField hf = (HiddenField)GridView1.Rows[e.RowIndex].FindControl("CategoryId");
                string sql = string.Format("DELETE tbl_DailyQuestion WHERE CategoryId={0};DELETE tbl_DailyCategory WHERE CategoryId={0}", hf.Value);
                database.ExecuteQuery(sql);
                LoadData();
                LiteralSuccess.Text = "Xóa thành công.";
            }
            catch (Exception ex)
            {
                LiteralError.Text = "Lỗi: " + ex.Message;
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            CheckerSelected();
        }
    }
}